<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                boardroomRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#boardroomGird").jqGrid('getGridParam', 'selrow');
                        var boardroom = jQuery("#boardroomGird").jqGrid('getRowData', rowid);
                        dijit.byId('boardroomUFD').setValues(boardroom);
                    }
                };
                topic.subscribe("boardroom/insertOrUpdate", boardroomRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#boardroomGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#boardroomGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#boardroomGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#boardroomGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                boardroomSubmitForm = function(formId, type) {
                    var url = "boardroom/insert";
                    if (type == 'u') {
                        url = "boardroom/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues());
                        syncDB(url, json);
                        form.hide();
                        topic.publish("boardroom/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                boardroomDelete = function() {
                    var ids = jQuery("#boardroomGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var boardroom = {};
                            boardroom.id = '';
                            array.forEach(ids, function(entry, i) {
                                boardroom.id += entry + ",";
                            });
                            syncDB("boardroom/delete", JSON.stringify(boardroom));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                boardroomQuery = function(formId) {                    
                    var containnum = dijit.byId('containnum').getValue();
                    var containnumMax = dijit.byId('containnumMax').getValue();
                    var roomname = dijit.byId('roomname').getValue();
                    var roomdesc = dijit.byId('roomdesc').getValue();
                    if(isNaN(containnum)){
                        containnum="";
                    }
                    if(isNaN(containnumMax)){
                        containnumMax="";
                    }
                    var boardroom = {
                            containnum:containnum,
                            containnumMax:containnumMax,
                            roomname:roomname,
                            roomdesc:roomdesc
                    }
                    if((containnumMax>=containnum)||(containnumMax==""||containnum=="")){
	                    jQuery("#boardroomGird").jqGrid('setGridParam', {
	                        postData : boardroom,
	                        mtype : "post"
	                    }).trigger('reloadGrid');
                    }else{
                        alert("人数范围错误");
                    }
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="boardroomIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="roomname">会议室名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="roomname"></td>
      </tr>
      <tr>
        <td><label for="roomdesc">会议室描述: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="roomdesc"></td>
      </tr>
      <tr>
        <td><label for="containnum">会议室容纳人数: </label></td>
        <td><input data-dojo-type="dijit/form/NumberTextBox" data-dojo-props="required: true, trim:true, invalidMessage: '只能输入数字'" type="text"
          name="containnum"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="boardroomSubmitForm('boardroomIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="boardroomRestForm('boardroomIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="boardroomUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="roomid" style="display: none;">
    <table>
      <tr>
        <td><label for="roomname">会议室名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="roomname"></td>
      </tr>
      <tr>
        <td><label for="roomdesc">会议室描述: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="roomdesc"></td>
      </tr>
      <tr>
        <td><label for="containnum">会议室容纳人数: </label></td>
        <td><input data-dojo-type="dijit/form/NumberTextBox" data-dojo-props="required: true, invalidMessage: '只能输入数字'" type="text"
          name="containnum"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="boardroomSubmitForm('boardroomUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="boardroomRestForm('boardroomUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryBoardRoomForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="roomname">会议室名称: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="roomname" id="roomname"></td>
          <td><label for="roomdesc">会议室描述: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="roomdesc" id="roomdesc"></td>
          <td><label for="containnum">会议室容纳人数: </label></td>
          <td>
              <input data-dojo-type="dijit/form/NumberTextBox" maxlength="3" style="width: 30px" data-dojo-props="trim: true" type="text" name="containnum" id="containnum">—
              <input data-dojo-type="dijit/form/NumberTextBox" maxlength="3" style="width: 30px" data-dojo-props="trim: true" type="text" name="containnumMax" id="containnumMax">
          </td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="boardroomInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('boardroomIFD').show();
      </script>
    </button>
    <button id="boardroomDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="boardroomDelete();"></button>
    <button id="boardroomUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#boardroomGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var boardroom = jQuery("#boardroomGird").jqGrid('getRowData', ids[0]);
          dijit.byId('boardroomUFD').setValues(boardroom);
          dijit.byId('boardroomUFD').show();
        }
      </script>
    </button>
    <button id="boardroomQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="boardroomQuery('queryBoardRoomForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="boardroomGird"></table>
  <div id="boardroomToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#boardroomGird").jqGrid({
        url : 'boardroom/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '会议室ID',
          '会议室名称',
          '会议室描述',
          '会议室容纳人数',
          '当前状态'
        ],
        colModel : [ 
        {
          name : 'roomid',
          index : 'roomid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'roomname',
          width : 100,
          align : 'center',
          index : 'roomname'
        },
        {
          name : 'roomdesc',
          width : 700,
          index : 'roomdesc'
        },
        {
          name : 'containnum',
          width : 100,
          align : 'center',
          index : 'containnum'
        },
        {
          name : 'nowstate',
          width : 100,
          align : 'center',
          index : 'nowstate'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#boardroomToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },gridComplete : function() {
            var ids = jQuery("#boardroomGird").jqGrid('getDataIDs');
            for(var i = 0 ; i < ids.length; i++){
                var cl = ids[i];
                var carcheck = jQuery("#boardroomGird").jqGrid('getRowData', cl);
                var nowstate = carcheck["nowstate"]=="" ? "此刻正在使用" : "空闲中";
                jQuery("#boardroomGird").jqGrid('setRowData', cl, {
                    nowstate : nowstate,
                });
            }
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "会议室管理列表"
    });
    jQuery("#boardroomGird").jqGrid('navGrid', '#boardroomToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
